import { NIcon } from 'naive-ui';

import * as fluent from "@vicons/fluent";
import * as ionicons4 from "@vicons/ionicons4";
import * as ionicons5 from "@vicons/ionicons5";
import * as antd from "@vicons/antd";
import * as fa from "@vicons/fa";
import * as material from "@vicons/material";
import * as tabler from "@vicons/tabler";
import * as carbon from "@vicons/carbon";

const groupList = [
  {name:'fluent',group:fluent},
  {name:'ionicons4',group:ionicons4},
  {name:'ionicons5',group:ionicons5},
  {name:'antd',group:antd},
  {name:'fa',group:fa},
  {name:'material',group:material},
  {name:'tabler',group:tabler},
  {name:'carbon',group:carbon}
];

interface Icon {
  key: string,
  group: 'fluent'|'ionicons4'|'ionicons5'|'antd'|'fa'|'material'|'tabler'|'carbon',
  icon: any,
  nicon: any,
}

const iconList = [] as Icon[];
const iconMap = {} as any;
const nicons = {} as any;

groupList.forEach(({name,group})=>{
  Object.keys(group).forEach((key)=>{
    const icon = ()=>h((group as any)[key]);
    const iconObj:Icon = {key: key, group: name as any, icon, nicon:()=>h(NIcon,icon)};
    iconList.push(iconObj);
  });
})

for(let i=iconList.length-1;i>=0;i--){
  const iconObj = iconList[i];
  iconMap[iconObj.group] = iconMap[iconObj.group] || {};
  iconMap[iconObj.group][iconObj.key] = iconObj;
  iconMap[iconObj.key] = iconObj;
  nicons[iconObj.group] = nicons[iconObj.group] || {};
  nicons[iconObj.group][iconObj.key] = iconObj.nicon;
  nicons[iconObj.key] = iconObj.nicon;
}

const nicon=(key:string,group?:string)=>{
  return (group?nicons[group][key]:nicons[key]) || nicons['QuestionCircle16Regular'];
}

export {
  iconList,
  iconMap,
  nicons,
  nicon
};